home *** CD-ROM | disk | FTP | other *** search
/ ...taking it to the Macs! / ...taking it to the Macs!.iso / Extras / ActiveX Mac SDK / ActiveX SDK / Containers / ContainerControl / CControlSite.cpp < prev    next >
Encoding:
Text File  |  1996-12-15  |  9.1 KB  |  347 lines  |  [TEXT/CWIE]

  1. // =================================================================================
  2. //    CControlSite.cpp            ©1996-97 Microsoft Corporation All rights reserved.
  3. // =================================================================================
  4. //
  5. //    A site for a container control
  6.  
  7. #include "ocheaders.h"
  8. #include "CControlContainer.h"
  9. #include "CControlSite.h"
  10.  
  11. //======================================================================
  12. //
  13. //    Public Methods
  14. //
  15.  
  16. #pragma mark === CControlSite::Construction & Destruction ===
  17.  
  18. // ---------------------------------------------------------------------------
  19. //        • CControlSite::CControlSite
  20. // ---------------------------------------------------------------------------
  21. //    Default Constructor
  22.  
  23. CControlSite::CControlSite(void) : CXSite()
  24. {
  25.     mContainerP = NULL;
  26.     mLocation.h = 0;
  27.     mLocation.v = 0;
  28.     mDimensions.h = 0;
  29.     mDimensions.v = 0;
  30. }
  31.  
  32.  
  33. // ---------------------------------------------------------------------------
  34. //        • CControlSite::CControlSite(CControlContainer*, Uint32, Uint32, Uint32, Uint32)
  35. // ---------------------------------------------------------------------------
  36. //    Construct site at the location specified within the container.
  37.  
  38. CControlSite::CControlSite(CControlContainer* inContainer, Uint32 inTop, Uint32 inLeft, Uint32 inHeight, Uint32 inWidth)
  39.     : CXSite()
  40. {
  41.     mContainerP = inContainer;
  42.     mLocation.h = inLeft;
  43.     mLocation.v = inTop;
  44.     mDimensions.h = inWidth;
  45.     mDimensions.v = inHeight;
  46. }
  47.  
  48.  
  49. // ---------------------------------------------------------------------------
  50. //        • CControlSite::~CControlSite
  51. // ---------------------------------------------------------------------------
  52. //    Destructor
  53.  
  54. CControlSite::~CControlSite()
  55. {
  56. }
  57.  
  58.  
  59. #pragma mark === CControlSite ===
  60.  
  61. // ---------------------------------------------------------------------------
  62. //        • CControlSite::Contains
  63. // ---------------------------------------------------------------------------
  64. //    Is the control hit by the point?
  65.  
  66. Boolean8 CControlSite::Contains (Point inWhere)
  67. {
  68.     // If point falls within the bounding rectangle of the site, then it's a hit.
  69.     if ((inWhere.h >= mLocation.h) && 
  70.         (inWhere.h <= mLocation.h + mDimensions.h) &&
  71.         (inWhere.v >= mLocation.v) && 
  72.         (inWhere.v <= mLocation.v + mDimensions.v))
  73.         return true;
  74.     else
  75.         return false;
  76. }
  77.  
  78.  
  79. // ---------------------------------------------------------------------------
  80. //        • CControlSite::EstablishContext
  81. // ---------------------------------------------------------------------------
  82. //    Set up the drawing environment for the control
  83.  
  84. ErrorCode CControlSite::EstablishContext(DrawContext* inContext, DrawContext* outContext)
  85. {
  86.     ErrorCode        theResult;
  87.  
  88.     if (!mHavePort)
  89.     {
  90.            Rect            clipRect;
  91.            RgnHandle        inClipRgn, controlClipRgn, outClipRgn;
  92.  
  93.         // Save off the current port state
  94.         SavePortState(inContext->Port);
  95.         mHavePort = true;
  96.  
  97.         // Figure out the clip rect
  98.         clipRect.top = inContext->Location.top;
  99.         clipRect.left = inContext->Location.left;
  100.         clipRect.bottom = clipRect.top + mDimensions.v;
  101.         clipRect.right = clipRect.left + mDimensions.h;
  102.         
  103.         // Set up the context
  104.         outContext->PortType = inContext->PortType;
  105.         outContext->DrawAspect = inContext->DrawAspect;
  106.         outContext->ContextID = inContext->ContextID;
  107.         outContext->Port = inContext->Port;
  108.         outContext->Location.left = clipRect.left;
  109.         outContext->Location.right = clipRect.right;
  110.         outContext->Location.top = clipRect.top;
  111.         outContext->Location.bottom = clipRect.bottom;
  112.         outContext->PrintRec = inContext->PrintRec;
  113.         outContext->ContainerRef = inContext->ContainerRef;
  114.         
  115.         // Initialize the port to a known state
  116.         ::SetOrigin(inContext->Port->portRect.left - mLocation.h, 
  117.                     inContext->Port->portRect.top - mLocation.v);
  118.  
  119.         // Adjust the clip
  120.         inClipRgn = ::NewRgn();
  121.         ::GetClip(inClipRgn);
  122.         ::OffsetRgn(inClipRgn, -(mLocation.h), -(mLocation.v));
  123.         controlClipRgn = ::NewRgn();
  124.         ::RectRgn(controlClipRgn, &clipRect);
  125.         outClipRgn = ::NewRgn();
  126.         ::SectRgn(inClipRgn, controlClipRgn, outClipRgn);
  127.         ::SetClip(outClipRgn);
  128.  
  129.         // Dispose of temporary data structures
  130.         ::DisposeRgn(outClipRgn);
  131.         outClipRgn = NULL;
  132.         ::DisposeRgn(controlClipRgn);
  133.         controlClipRgn = NULL;
  134.         ::DisposeRgn(inClipRgn);
  135.         inClipRgn = NULL;
  136.         
  137.         theResult = S_OK;
  138.     }
  139.     else
  140.         theResult = E_FAIL;
  141.     
  142.     return theResult;
  143. }
  144.  
  145.  
  146. //----------------------------------------------------------------------
  147. //
  148. //    IContainerSite Methods
  149. //
  150.  
  151. #pragma mark === CControlSite::IContainerSite ===
  152.  
  153. // ---------------------------------------------------------------------------
  154. //        • CControlSite::IContainerSite::RequestFocus
  155. // ---------------------------------------------------------------------------
  156. //  Pass request to container
  157.  
  158. STDMETHODIMP
  159. CControlSite::RequestFocus ( Boolean inAcquire, FocusSet inFocus)
  160. {
  161.     ErrorCode    theResult;
  162.     
  163.     if (mContainerP)
  164.         theResult = mContainerP->RequestFocus(this, inAcquire, inFocus);
  165.     else
  166.         theResult = E_FAIL;
  167.     
  168.     return theResult;
  169. }
  170.  
  171. // ---------------------------------------------------------------------------
  172. //        • CControlSite::IContainerSite::AcquireContext
  173. // ---------------------------------------------------------------------------
  174. //  Set up drawing environment for control within context of container's environment.
  175.  
  176. STDMETHODIMP
  177. CControlSite::AcquireContext(Uint32 inContextID, DrawContext* outContext)
  178. {    
  179.     ErrorCode            theResult = S_OK;
  180.     IContainerSite*        siteP;
  181.  
  182.     if (!mHavePort && 
  183.         mUnkOuter && 
  184.         SUCCEEDED(mUnkOuter->QueryInterface(IID_IContainerSite, &siteP)))
  185.     {
  186.         if (siteP->AcquireContext(inContextID, &mContext) == S_OK)
  187.         {
  188.             theResult = EstablishContext(&mContext, outContext);
  189.         }
  190.         else
  191.         {
  192.             outContext->Port = NULL;
  193.             theResult = E_FAIL;
  194.         }
  195.         
  196.         siteP->Release();        
  197.     }
  198.     else
  199.     {
  200.         outContext->Port = NULL;
  201.         theResult = E_FAIL;
  202.     }
  203.     
  204.     return theResult;
  205. }
  206.  
  207.  
  208. // ---------------------------------------------------------------------------
  209. //        • CControlSite::IContainerSite::ReleaseContext
  210. // ---------------------------------------------------------------------------
  211. //    Restore the drawing environment and release our container's context
  212.  
  213. STDMETHODIMP
  214. CControlSite::ReleaseContext(DrawContext* inContext)
  215. {
  216.     ErrorCode            theResult;
  217.     IContainerSite*        siteP;
  218.     
  219.     theResult = inherited::ReleaseContext(inContext);
  220.     if ( theResult == S_OK)
  221.     {
  222.         if (SUCCEEDED(mUnkOuter->QueryInterface(IID_IContainerSite, &siteP)))
  223.         {
  224.             theResult = siteP->ReleaseContext(&mContext);
  225.             siteP->Release();
  226.         }
  227.     }
  228.     
  229.     return theResult;
  230. }
  231.  
  232.  
  233. // ---------------------------------------------------------------------------
  234. //        • CControlSite::IContainerSite::SetIdleTime
  235. // ---------------------------------------------------------------------------
  236. //    schedule some idle time for the control
  237.  
  238. STDMETHODIMP
  239. CControlSite::SetIdleTime(Int32 inWaitTicks, Uint32 inIdleRefCon)
  240. {
  241.     ErrorCode            theResult;
  242.  
  243.     if (mContainerP)
  244.         theResult = mContainerP->SetIdleTime(this, inWaitTicks, inIdleRefCon);
  245.     else
  246.         theResult = E_FAIL;
  247.  
  248.     return theResult;
  249. }
  250.  
  251.  
  252. //----------------------------------------------------------------------
  253. //
  254. //    IControl Methods
  255. //
  256.  
  257. #pragma mark === CControlSite::IControl ===
  258.  
  259. // ---------------------------------------------------------------------------
  260. //        • CControlSite::IControl::Draw
  261. // ---------------------------------------------------------------------------
  262. //    Set up the control's drawing environment and tell it to draw
  263.  
  264. STDMETHODIMP
  265. CControlSite::Draw(DrawContext* inContext)
  266. {
  267.     ErrorCode    theResult = S_OK;
  268.     
  269.     if ( mControlP )
  270.     {
  271.         DrawContext            outContext = {BeginPortType};
  272.         
  273.         if (EstablishContext(inContext, &outContext) == S_OK)
  274.         {
  275.             theResult = mControlP->Draw(&outContext);
  276.             inherited::ReleaseContext(&outContext);
  277.         }
  278.         else
  279.         {
  280.             theResult = E_FAIL;
  281.         }
  282.     }
  283.     else
  284.         mCachedDraw = true;
  285.     
  286.     return theResult;
  287. }
  288.  
  289. // ---------------------------------------------------------------------------
  290. //        • CControlSite::IControl::OnContextChange
  291. // ---------------------------------------------------------------------------
  292. //    Set up the control's drawing environment and pass on the context change command
  293.  
  294. STDMETHODIMP
  295. CControlSite::OnContextChange(UInt32 inContextID, ContextCommand inCommand)
  296. {
  297.     ErrorCode    theResult = S_OK;
  298.     
  299.     if ( mControlP )
  300.     {
  301.         DrawContext            outContext = {BeginPortType};
  302.         
  303.         //if (EstablishContext(inContext, &outContext) == S_OK)
  304.         {
  305.         theResult = mControlP->OnContextChange(inContextID, inCommand);
  306.             //inherited::ReleaseContext(&outContext);
  307.         }
  308.         //else
  309.         //{
  310.         //    theResult = E_FAIL;
  311.         //}
  312.     }
  313.     else
  314.         mCachedContextChange = true;
  315.  
  316.        return theResult;
  317. }
  318.  
  319.  
  320. // ---------------------------------------------------------------------------
  321. //        • CControlSite::IControl::DoActivate
  322. // ---------------------------------------------------------------------------
  323. //    Set up the control's drawing environment and pass on the activate command
  324.  
  325. STDMETHODIMP
  326. CControlSite::DoActivate(ActivateEventType inActiveET, UInt32 inContextID, PlatformEvent* inEvent)
  327. {
  328.     ErrorCode    theResult = S_OK;
  329.     
  330.     if ( mControlP )
  331.     {
  332.         DrawContext            outContext = {BeginPortType};
  333.         
  334.         //if (EstablishContext(inContext, &outContext) == S_OK)
  335.         {
  336.             theResult = mControlP->DoActivate(inActiveET, inContextID, inEvent);
  337.             //inherited::ReleaseContext(&outContext);
  338.         }
  339.         //else
  340.         //{
  341.         //    theResult = E_FAIL;
  342.         //}
  343.     }
  344.  
  345.        return theResult;
  346. }
  347.